﻿/*
 * fbasync - An Asynchronous Facebook API for .NET
 * Copyright (C) 2007 ittybittysoft
 * Email: info@ittybittysoft.com
 * Portions of the library are based on the Facebook Developer Toolkit
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Xml;

namespace fbasync
{
    [DataContract(), Serializable()]
    public class Photo
        : FacebookEntity
    {
        public readonly static IEnumerable<string> AllFields =
            new string[] { "pid", "aid", "owner", "src_small", "src_big", "src", "link", "caption", "created" };

        public const string DefaultOrderBy = "created DESC";
        public const string FacebookEntity = "photo";

        [DataMember()]
        public string pid { get; set; }

        [DataMember()]
        public string aid { get; set; }

        [DataMember()]
        public long? owner { get; set; }

        [DataMember()]
        public string src_small { get; set; }

        [DataMember()]
        public string src_big { get; set; }

        [DataMember()]
        public string src { get; set; }

        [DataMember()]
        public string link { get; set; }

        [DataMember()]
        public string caption { get; set; }

        [DataMember()]
        public DateTime? created { get; set; }

        public Uri GetBiggestSrc()
        {
            if (!string.IsNullOrEmpty(src_big))
                return new Uri(src_big, UriKind.Absolute);
            else if (!string.IsNullOrEmpty(src))
                return new Uri(src, UriKind.Absolute);
            else if (!string.IsNullOrEmpty(src_small))
                return new Uri(src_small, UriKind.Absolute);
            else
                return null;
        }

        public static IList<Photo> FromFqlResult(XmlDocument fqlResult, bool keepXml)
        {
            List<Photo> results = fqlResult.GetFqlResults(keepXml, e =>
                new Photo
                {
                    aid = e.GetChildText("aid"),
                    caption = e.GetChildText("caption"),
                    created = e.GetChildUnixTime("created"),
                    link = e.GetChildText("link"),
                    owner = e.GetChildLong("owner"),
                    pid = e.GetChildText("pid"),
                    src = e.GetChildText("src"),
                    src_big = e.GetChildText("src_big"),
                    src_small = e.GetChildText("src_small")
                });
            return results;
        }
    }
}
